import datetime
from database.common import cursor

def select_user_id(user_id):
    sql = """
    SELECT * FROM user_info WHERE user_id = %s
    """
    cursor.execute(sql, (user_id,))
    return cursor.fetchone()

def select_user_by_user_id(user_id):
    sql = """
    SELECT * FROM user_info WHERE user_id = %s
    """
    cursor.execute(sql, (user_id,))
    return cursor.fetchone()

def insert_user(user_class):
    now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    sql = """
    INSERT INTO user_info (user_id, name, followers_count, friends_count, 
                           statuses_count, verified, location, avatar_weibo_url,
                           is_avatar, comment_count, repost_count, like_count,
                           social_activity_total, create_time, update_time) VALUES 
                           (%s, %s, %s, %s, 
                           %s, %s, %s, %s, 
                           %s, %s, %s, %s, 
                           %s, %s, %s)
    """
    params = (
        user_class.user_id, user_class.name, user_class.followers_count, user_class.friends_count, 
        user_class.statuses_count, user_class.verified, user_class.location, user_class.avatar_weibo_url,
        user_class.is_avatar, user_class.comment_count, user_class.repost_count, user_class.like_count, 
        user_class.social_activity_total, now, now
    )
    
    cursor.execute(sql, params)

def update_user(user_class):
    sql = """
    UPDATE user_info SET name = %s, followers_count = %s, friends_count = %s,
                        statuses_count = %s, location = %s, avatar_weibo_url = %s,  
                        is_avatar = %s, comment_count = %s, repost_count = %s, like_count = %s,
                        social_activity_total = %s where user_id = %s
    """
    params = (
        user_class.name, user_class.followers_count, user_class.friends_count, 
        user_class.statuses_count, user_class.location, user_class.avatar_weibo_url,
        user_class.is_avatar, user_class.comment_count, user_class.repost_count, user_class.like_count, 
        user_class.social_activity_total, user_class.user_id
    )
    # 输出sql
    # 直接获取完整SQL字符串（无需decode）
    complete_sql = cursor.mogrify(sql, params)  # 移除.decode('utf-8')
    print("完整SQL:", complete_sql)
    
    cursor.execute(sql, params)
